home *** CD-ROM | disk | FTP | other *** search
/ Capture the Power of the Internet / Capture the Power of the Internet.iso / mac / MacFiles / System / Calculators / mathpad.sit / MathPad 2.5.2 / Documentation / Strings / Strings
Encoding:
Text File  |  2000-04-03  |  3.5 KB  |  87 lines  |  [TEXT/MPad]

  1.  
  2. -------------- Strings
  3.  
  4. --- String Constants
  5.  
  6. Text strings are used for file names and plot labels. Strings are enclosed in double quotes and can contain blanks or punctuation but they cannot contain a RETURN. 
  7. Variables can be defined or assigned as strings. If the variable is to be evaluated many times it is better to assign the string so that multiple copies of the string do not need to be created. 
  8.  
  9. --- sprintf
  10.  
  11. Numeric results can be converted to strings with the built-in function sprintf(format,n). This function is essentially the same as the C standard library function. 
  12. The format parameter is a string which controls the conversion. The format must contain a single conversion specification. Any characters that are not part of the conversion specification are copied directly to the result string. A conversion specification begins with a % character. A % character can be ouput by using %%. 
  13. The conversion has the following form: (brackets indicate optional values).
  14.  
  15. ╩%[flags][size][.precision]conversion
  16.  
  17. flag
  18. -       left justify
  19. +       include leading plus
  20. space   include a leading space
  21. 0       pad with leading zeros
  22. #       use alternate form
  23.  
  24. size
  25. minimum number of output characters
  26. The field is padded with spaces if needed.
  27.  
  28. precision
  29. floating point   number of digits after the decimal point
  30. integer          minimum number of digits
  31. string           maximum number of characters
  32.  
  33. conversion
  34. character       n               output                  # alternate form 
  35. c               ascii code      single character 
  36. d               integer         signed integer 
  37. e E             floating point  E format                always include point
  38. f               floating point  fixed point format      always include point
  39. g G             floating point  smaller of f or e       include trailing zeros
  40. i               integer signed  integer
  41. o               integer octal   force leading zero
  42. s               string          string                  not allowed
  43. u               integer         unsigned integer
  44. x X             integer         hexadecimal             use 0x prefix
  45.  
  46.  
  47. examples:
  48.  
  49. format          sprintf(format,42) 
  50. "n=%f"          "n=42.000000"
  51. "n=%e"          "n=4.200000e+01"
  52. "n=%E"          "n=4.200000E+01"
  53. "n=%g"          "n=42"
  54. "n=%.2f"        "n=42.00"
  55. "n=%10.2f"      "n=     42.00"
  56. "n=%-10.2f"     "n=42.00     "
  57. "n=%d"          "n=42"
  58. "n=%X"          "n=2A"
  59. "n=%o"          "n=52"
  60. "n=%c"          "n=*"
  61.  
  62.  
  63. --- Concatenation
  64.  
  65. The sprintf function only allows one conversion in the format string but results can be joined together with the concatenation operator '|' . Both operands must be strings. 
  66.  
  67. "what" | "not":"whatnot"
  68.  
  69.  
  70. --- String Output
  71.  
  72. String results are shown in quotes. The table command can be used to output arrays with string elements. 
  73.  
  74. weekdays = {"mon","tues","wed","thurs","fri"}
  75.  
  76. table {weekdays}
  77. ╩╩╩╩"mon"╩╩╩╩"tues"╩╩╩╩"wed" ╩╩╩╩"thurs"╩╩╩╩"fri"
  78.  
  79. The write() function will write out strings for the special case of a 2D array of strings and a TEXT file. Each string is written out with no quotes or separators. A line in the output file corresponds to a row in the 2D array. 
  80. An explicit index on a string will extract the ASCII value of a single character 
  81.  
  82. "tues"[1]:116 -- 116 is the ASCII code for 't'
  83. "tues"[2]:117 -- 117 is the ASCII code for 'u'
  84.  
  85. Strings are not general purpose objects. They are handled in the above special cases. Most other operations will have no effect on the string and will not generate any error messages. In cases where a string is not allowed, an undefined or an array of ASCII values is used.   
  86.  
  87.